package com.vehicle.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vehicle.bean.DataGridView;
import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.MaintainMapper;
import com.vehicle.mapper.MaintainRecordMapper;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.MaintainRecord;
import com.vehicle.service.MaintainRecordService;
import com.vehicle.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MaintainRecordServiceImpl implements MaintainRecordService {
    @Autowired
    private MaintainRecordMapper maintainRecordMapper;
    private SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    private CarMapper carMapper;
    @Override
    public DataGridView findAllMaintainRecord(MaintainRecord maintainRecord) {
        DataGridView dataGridView = new DataGridView();
        Page page = PageHelper.startPage(maintainRecord.getPage(),maintainRecord.getLimit());
        List<MaintainRecord> maintainRecords=maintainRecordMapper.selectAllMaintainRecord(maintainRecord);
        dataGridView.setCount(page.getTotal());
        dataGridView.setData(maintainRecords);
        return dataGridView;
    }

    @Override
    public DataGridView findMaintainRecord(MaintainRecord maintainRecord) {
        DataGridView dataGridView =new DataGridView();
        dataGridView.setData(maintainRecordMapper.selectAllMaintainRecord(maintainRecord));
        return dataGridView;
    }

    /**
     * 根据id删除
     * @param maintainid
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMaintainRecord(int maintainid) {
        maintainRecordMapper.updateStatusByMaintainId(maintainid);
    }

    @Override
    public void deleteBatchByIds(Integer[] maintainIds) {
        for (Integer maintainId:maintainIds){
            maintainRecordMapper.updateStatusByMaintainId(maintainId);
        }
    }

    /**
     * 增加保养记录
     * @param maintainRecord
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addMaintainRecord(MaintainRecord maintainRecord) {
        //根据carid查询Car的保养km
        Car car=carMapper.selectCarByCarid(maintainRecord.getCars().getCarid());
        //需要修改的Car  km=查询到的+新的保养km
        Car updateCar = new Car();
        //设置Car的保养里程
        updateCar.setCar_servicekm(car.getCar_startkm());
        updateCar.setCarid(maintainRecord.getCars().getCarid());
        //设置下次保养里程 =查到的起始里程+5000
        maintainRecord.setNext_maintainkm(maintainRecord.getMaintainkm()+5000);
        //设置下次保养时间
        Date date=maintainRecord.getMaintaintime();
        String strDate=DateUtil.processingTime(date,6);
        try {
            maintainRecord.setNext_maintaintime(simpleDateFormat.parse(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        carMapper.updateCarServiceKM(updateCar);
        maintainRecordMapper.addMaintainRecord(maintainRecord);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateMaintainRecord(MaintainRecord maintainRecord) {
        //根据carid查询Car的保养km
        Car car=carMapper.selectCarByCarid(maintainRecord.getCars().getCarid());
        //需要修改的Car  km=查询到的+新的保养km
        Car updateCar = new Car();
        //设置Car的保养里程
        updateCar.setCar_servicekm(car.getCar_startkm());
        updateCar.setCarid(maintainRecord.getCars().getCarid());
        //设置下次保养里程 =查到的起始里程+5000
        maintainRecord.setNext_maintainkm(maintainRecord.getMaintainkm()+5000);
        //设置下次保养时间
        Date date=maintainRecord.getMaintaintime();
        String strDate=DateUtil.processingTime(date,6);
        try {
            maintainRecord.setNext_maintaintime(simpleDateFormat.parse(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        carMapper.updateCarServiceKM(updateCar);
        maintainRecordMapper.updateMaintainRecord(maintainRecord);
    }
}
